package com.example.kotlinlearndemo

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.DynamicDrawableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.ImageSpan
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity2 : AppCompatActivity() {
    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        val test = findViewById<TextView>(R.id.test)

        val nick: String = "昵称"
        val userBaseInfo: String = "基础信息"

        val textStyle = SpannableStringBuilder()
        val foregroundColorSpan =
            ForegroundColorSpan(Color.parseColor("#49d2ff"))
        val foregroundColorSpan2 = ForegroundColorSpan(Color.TRANSPARENT)

        val nickSpan = AbsoluteSizeSpan(28)
        val infoSpan = AbsoluteSizeSpan(24)

        textStyle
            .append(nick)
            .append(" ")
            .append(userBaseInfo)

        val startPosition = 0
        textStyle.setSpan(
            foregroundColorSpan, startPosition, textStyle.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        textStyle.setSpan(nickSpan, 0, nick.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        textStyle.setSpan(infoSpan, nick.length, textStyle.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

        textStyle.insert(0, "vip admin ")

        val t1:Boolean=false
        val t2:Boolean=false

        test.let {
            if(t1) {
                val adminDrawable: Drawable =
                    resources.getDrawable(R.mipmap.icon_room_chat_medium_male)
                adminDrawable.setBounds(
                    0,
                    0,
                    adminDrawable.intrinsicWidth,
                    adminDrawable.intrinsicHeight
                )
                textStyle.setSpan(
                    ImageSpan(adminDrawable, DynamicDrawableSpan.ALIGN_CENTER),
                    4,
                    4 + 5,
                    SpannableString.SPAN_INCLUSIVE_INCLUSIVE
                )
            }else{
                textStyle.delete(4,4+6)
            }
        }

        test.let {
            if(t2) {
                val adminDrawable: Drawable =
                    resources.getDrawable(R.mipmap.icon_room_chat_medium_male)
                adminDrawable.setBounds(
                    0,
                    0,
                    adminDrawable.intrinsicWidth,
                    adminDrawable.intrinsicHeight
                )
                textStyle.setSpan(
                    ImageSpan(adminDrawable, DynamicDrawableSpan.ALIGN_CENTER),
                    0,
                    3,
                    SpannableString.SPAN_INCLUSIVE_INCLUSIVE
                )
            }else{
                textStyle.delete(0,4)
            }
        }

        test.text = textStyle
    }
}